package org.mymail.mail;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.mail.Address;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import noNamespace.FreskoSubscribersDocument;
import org.apache.xmlbeans.XmlException;

public class Recipients {

	public static noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber subscribers[];
	
	static {
		try {
			FreskoSubscribersDocument recipientsDoc = noNamespace.FreskoSubscribersDocument.Factory
					.parse(new File(MailConstants.recipientsXMLLoc));
			noNamespace.FreskoSubscribersDocument.FreskoSubscribers s = recipientsDoc
					.getFreskoSubscribers();
			subscribers = s.getSubscriberArray();
			System.err.println("******************* NO OF SUBSCRIBERS = "+subscribers.length);
		} catch (XmlException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static HashMap<Address, SubscriberDetails> getAddresses(
			int mailFwdType) {
		HashMap<Address, SubscriberDetails> addrDetails = new HashMap<Address, SubscriberDetails>();
		try {
			for (int i = 0; i < subscribers.length; i++) {
				noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber sub = subscribers[i];
				noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType subType[] = sub
						.getSubscribeTypeArray();
				boolean isSubscribed = isSubscribedTo(subType, mailFwdType);
				if (isSubscribed) {
					SubscriberDetails subDet = new SubscriberDetails();
					subDet.setSubscribedBy(sub.getSubscribedBy());
					subDet.setName(sub.getName());
					String mailId = sub.getId();
					Address a = new InternetAddress(mailId);
					addrDetails.put(a, subDet);
				}
			}

		} catch (AddressException e) {
			e.printStackTrace();
		}
		return addrDetails;
	}

	public static boolean isSubscribedTo(
			noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType subType[],
			int mailFwdType) {
		for (int i = 0; i < subType.length; i++) {
			noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType subscribeType = subType[i];
			if (subscribeType.getId() == mailFwdType)
				return true;
		}

		return false;
	}

	/*	public static String getName(Address address) {
	 String name = "";
	 for (int i = 0; i < subscribers.length; i++) {
	 if (!address.toString().equalsIgnoreCase(subscribers[i].getId()))
	 continue;
	 name = subscribers[i].getName();
	 if (name == "" || name == null)
	 name = subscribers[i].getId();
	 break;
	 }

	 return name;
	 }*/

	public static int getRecipientsFileDetails(Address address2, int mailFwdType) {
		int fileId = 0;
		for (int i = 0; i < subscribers.length; i++) {
			noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber sub = subscribers[i];
			if (sub.getId().equalsIgnoreCase(address2.toString())) {
				noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType subtype[] = sub
						.getSubscribeTypeArray();
				for (int j = 0; j < subtype.length; j++) {
					noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType subscribeType = subtype[j];
					if (subscribeType.getId() == mailFwdType)
						fileId = subscribeType.getLastReceived();
				}

			}
		}

		return ++fileId;
	}

}